import { Component, Inject, ChangeDetectionStrategy } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MAT_DIALOG_DATA, MatDialogRef, MatDialogModule } from '@angular/material/dialog';
import { MatButtonModule } from '@angular/material/button';
import { MatExpansionModule } from '@angular/material/expansion';
import { TranslateModule } from '@ngx-translate/core';
import { DialogComponent } from '@shared/components/dialog.component';
import { SafePipe } from '@shared/pipe/safe.pipe';

export interface ErrorAlertDialogData {
  title: string;
  message: string;
  error: any;
  ok: string;
}

/**
 * 错误提示对话框组件
 * 
 * 显示错误信息对话框，包含错误消息和详细堆栈信息
 */
@Component({
  selector: 'cne-error-alert-dialog',
  standalone: true,
  imports: [CommonModule, MatDialogModule, MatButtonModule, MatExpansionModule, TranslateModule, SafePipe],
  changeDetection: ChangeDetectionStrategy.OnPush,
  templateUrl: './error-alert-dialog.component.html',
  styleUrls: ['./error-alert-dialog.component.scss']
})
export class ErrorAlertDialogComponent extends DialogComponent<ErrorAlertDialogComponent, boolean> {

  title: string;
  message: string;
  errorMessage: string;
  errorDetails?: string;

  constructor(
    public override dialogRef: MatDialogRef<ErrorAlertDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: ErrorAlertDialogData
  ) {
    super(dialogRef);
    this.title = this.data.title;
    this.message = this.data.message;
    this.errorMessage = this.data.error.message ? this.data.error.message : JSON.stringify(this.data.error);
    // 将堆栈信息中的换行符转换为 HTML <br/> 标签
    if (this.data.error.stack) {
      this.errorDetails = this.data.error.stack.replace(/\n/g, '<br/>');
    }
  }

}
